<template>
  <!-- 个人实名认证2 》 人脸识别 -->
  <view class="content-real">
    <view class="con_title" style="padding-top: 74rpx"
      >请确认本人
      <text class="color-blue">{{ nameDesensitization(params?.name || '') }}</text>
      身份信息
    </view>
    <view class="con_title">请保持正脸在取景框中根据屏幕指示完成识别</view>
    <image class="camera" src="@/static/my/face-recognition-git-2.png" />
    <!-- 提示 -->
    <view class="con_msg">
      <view class="con_msg_item">
        <image src="@/static/my/auth-icon-phone.png" />
        <view class="text">正对手机</view>
      </view>
      <view class="con_msg_item">
        <image src="@/static/my/auth-icon-bulb.png" />
        <view class="text">光线充足</view>
      </view>
      <view class="con_msg_item">
        <image src="@/static/my/auth-icon-face.png" />
        <view class="text">脸无遮挡</view>
      </view>
    </view>
    <view class="startFace" @click="startFace">开始人脸识别</view>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import { onLoad, onReachBottom, onReady } from '@dcloudio/uni-app'
import {
  showToast,
  navigateTo,
  navigateBack,
  formatDate,
  nameDesensitization,
  showLoading,
  hideLoading,
} from '@/utils'
import { uploadApi } from '@/utils/upLoad.ts'
import { getFaceInfoApi, faceAuthApi } from '@/services/my/user/index.ts'
import { useMemberStore } from '@/stores'
import { getDoctorAudit, getDoctorFace } from '@/api/business'
const params = ref({}) // 传递过来的参数 name、idcard、idcard_just、idcard_back
onLoad((options) => {
  params.value = JSON.parse(options.info)
})

// 开始人脸识别
const startFace = async () => {
  // #ifdef APP-PLUS || APP-IOS || APP-ANDROID
  try {
    showLoading('加载中...')
    let face = uni.requireNativePlugin('DC-WBFaceService')
    const res = await getFaceInfoApi({
      // name: params.value.name,
      // idcard: params.value.idcard,
      name: '贾贝',
      idcard: '411381199905185314',
      cate: 2,
    })
    // const { appId, nonce, sign, orderNo, licence, faceId, apiVersion, compareType, userId } = res.data
    const { appId, nonce, sign, orderNo, licence, faceId, apiVersion, compareType } = res.data

    const userId = useMemberStore().userInfo.uid || ''

    console.log('人脸识别插件所需参数：', res.data)
    console.log('userId', userId)
    face.startWbFaceVerifyService(
      {
        userId: userId, //用户id
        nonce: nonce, //随机数
        sign: sign, //签名
        appId: appId, //appId
        orderNo: orderNo, //订单号
        apiVersion: apiVersion, //api版本号
        licence: licence, //授权码
        compareType: compareType, //比对类型，0：不比对，1：比对
        faceId: faceId, //人脸id
        sdkConfig: {
          //和iOS共有的配置参数
          showSuccessPage: false, //是否展示成功页面,默认不展示
          showFailurePage: true, //是否展示失败页面,默认不展示
          recordVideo: false, //是否录制视频,默认不录制
          checkVideo: false, //是否检查视频大小
          playVoice: true, //是否播放语音提示，默认不播放
          theme: '1', //sdk皮肤设置，0黑色，1白色
          customerTipsLoc: '0', //自定义提示的位置，0在预览框下面，1在预览框上面
          customerTipsInLive: '正在检测...', //检测时的自定义提示语
          customerTipsInUpload: '识别中...', //上传时的自定义提示语
          customerLongTip: '', //自定义长提示语
          //android独有的配置参数
          isEnableLog: true, //是否打开刷脸native日志，请release版本关闭！！！
          //iOS独有的配置参数
          windowLevel: '1', //sdk中拉起人脸活体识别界面中使用UIWindow时的windowLevel配置
          manualCookie: true, //是否由SDK内部处理sdk网络请求的cookie
          useWindowSecene: false,
        },
      },
      (result) => {
        console.log('SDK callback:1', result)
        // uni.showToast({
        //     title: result,
        //     icon: 'none',
        //     duration: 10000,
        // })
        hideLoading()
        if (result.res.success) {
          faceAuth({
            name: params.value.name,
            idcard: params.value.idcard,
            idcard_front: params.value.idcard_just,
            idcard_back: params.value.idcard_back,
            faceid: faceId,
            order_no: result.res.orderNo,
            similarity: result.res.similarity,
            //   user_img: result.res.userImageString, // 用户图像 字符串信息
          })
          subFace(1)
        } else {
          subFace(0)
          console.log('result.------2 else', result)
          showToast(
            `${result?.res?.error?.code}:${result?.res?.error?.reason}`,
            'none',
            false,
            3000,
          )
          // navigateTo('/pages-user/tool-real/real-fail')
        }
      },
    )
  } catch (err) {
    // uni.showToast({
    //     title: err,
    //     icon: 'none',
    //     duration: 10000,
    // })
    hideLoading()
    showToast('人脸识别失败')
    subFace(2)
  }
  // #endif

  // #ifdef H5
  showToast('请在APP中进行人脸识别')
  // #endif
}
async function subFace(num) {
  const res = await getDoctorFace({
    type: num,
  })
}
async function faceAuth(params) {
  console.log('实名认证最后的提交前：', params)
  const res = await getDoctorAudit({
    name: userInfo.value.name,
    idCard: userInfo.value.idCard,
    phone: userInfo.value.phone,
    hospital_name: userInfo.value.hospital_name,
    rf_name: userInfo.value.rf_name,
    doctor_practicing_certificate: userInfo.value.doctor_practicing_certificate,
    doctor_qualification: userInfo.value.doctor_qualification,
    type:2
  })
  console.log('最后的提交后：', res)
  if (res.code == 200) {
    navigateTo('/pages-users/tool-real/real-success')
  }
  showToast(res.message || '提交失败')
}
</script>

<style scoped lang="scss">
.color-blue {
  color: #ffb300;
}

.content-real {
  width: 100%;
  min-height: 100vh;
  background: #f6f6f6;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  align-items: center;

  .startFace {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 130rpx;
    width: 630rpx;
    height: 91rpx;
    background: #ffb300;
    border-radius: 46rpx 46rpx 46rpx 46rpx;
    opacity: 1;
    font-size: 34rpx;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    color: #f6f6f6;
  }

  .con_msg {
    margin-top: 150rpx;
    height: 150rpx;
    width: 750rpx;
    padding: 0 60rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .con_msg_item {
      flex: 1;
      height: 100%;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: space-between;

      image {
        width: 68rpx;
        height: 68rpx;
      }

      .text {
        font-size: 28rpx;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        color: #000000;
      }
    }
  }

  .con_title {
    font-size: 24rpx;
    font-family: PingFang-SC-Medium, PingFang-SC;
    font-weight: 500;
    color: #7d7d7d;
    line-height: 33rpx;
    margin: 0 auto;
    text-align: center;
  }

  .camera {
    margin-top: 100rpx;
    width: 424rpx;
    height: 424rpx;
    border-radius: 50%;
  }
}
</style>
